<!DOCTYPE html>
<html lang="zh-CN" xmlns="">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理系统</title>
    <!-- 引入 Vue 2 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!-- 引入 Element UI -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

    <style>
        .app-container {
            padding: 20px;
        }
        .mb8 {
            margin-bottom: 8px;
        }
        .el-form-item {
            margin-bottom: 12px;
        }
    </style>
</head>
<body>
<div id="app" class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="用户名" prop="username">
            <el-input
                    v-model="queryParams.username"
                    placeholder="请输入用户名"
                    clearable
                    @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input
                    v-model="queryParams.password"
                    placeholder="请输入密码"
                    clearable
                    @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item label="电子邮件" prop="email">
            <el-input
                    v-model="queryParams.email"
                    placeholder="请输入电子邮件"
                    clearable
                    @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
            <el-input
                    v-model="queryParams.phone"
                    placeholder="请输入电话号码"
                    clearable
                    @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item label="角色ID" prop="roleId">
            <el-input
                    v-model="queryParams.roleId"
                    placeholder="请输入角色ID"
                    clearable
                    @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
            >新增</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button
                    type="success"
                    plain
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
            >修改</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
            >删除</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="toggleSearch"
            >{{ showSearch ? '隐藏搜索' : '显示搜索' }}</el-button>
        </el-col>
    </el-row>

<!--   <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" style="width: 100%">-->
<!--        <el-table-column type="selection" width="55" align="center"/>-->
<!--        <el-table-column label="用户ID" align="center" prop="userId" width="100"/>-->
<!--        <el-table-column label="用户名" align="center" prop="username" width="100"/>-->
<!--       <el-table-column label="密码" align="center" prop="password" width="55"/>-->
<!--        <el-table-column label="电子邮件" align="center" prop="email" width="200"/>-->
<!--        <el-table-column label="电话号码" align="center" prop="phone" width="150"/>-->
<!--        <el-table-column label="角色ID" align="center" prop="roleId" width="100"/>-->
<!--        <el-table-column label="用户状态" align="center" prop="status" width="80"/>-->
<!--        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="150">-->
<!--            <template slot-scope="scope">-->
<!--                <el-button-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-edit"-->
<!--                        @click="handleUpdate(scope.row)"-->
<!--                >修改</el-button>-->
<!--                <el-button-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-delete"-->
<!--                        @click="handleDelete(scope.row)"-->
<!--                >删除</el-button>-->
<!--            </template>-->
<!--        </el-table-column>-->
<!--    </el-table>-->

    <el-table
            ref="multipleTable"
            :data="userList"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="username"
                label="用户名"
                width="120">
        </el-table-column>
        <el-table-column
                prop="email"
                label="电子邮件"
                show-overflow-tooltip>
        </el-table-column>
        <el-table-column
                prop="phone"
                label="电话号码"
                show-overflow-tooltip>
        </el-table-column>
        <el-table-column
                prop="roleId"
                label="角色ID"
                show-overflow-tooltip>
        </el-table-column>
        <el-table-column
                prop="status"
                label="用户状态"
                show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="150">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                >删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            v-show="total>0"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParams.pageNum"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryParams.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" placeholder="请输入密码" show-password />
            </el-form-item>
            <el-form-item label="电子邮件" prop="email">
                <el-input v-model="form.email" placeholder="请输入电子邮件" />
            </el-form-item>
            <el-form-item label="电话号码" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入电话号码" />
            </el-form-item>
            <el-form-item label="角色ID" prop="roleId">
                <el-input v-model="form.roleId" placeholder="请输入角色ID" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</div>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!-- 引入 axios -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    // API请求方法
    const api = {
        listUser(query) {
            return axios({
                url: '/system/user/list',
                method: 'get',
                params: query
            })
        },
        getUser(userId) {
            return axios({
                url: '/system/user/' + userId,
                method: 'get'
            })
        },
        addUser(data) {
            return axios({
                url: '/system/user',
                method: 'post',
                data: data
            })
        },
        updateUser(data) {
            return axios({
                url: '/system/user',
                method: 'put',
                data: data
            })
        },
        delUser(userId) {
            return axios({
                url: '/system/user/' + userId,
                method: 'delete'
            })
        }
    };

    new Vue({
        el: '#app',
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 用户表格数据
                userList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    username: null,
                    password: null,
                    email: null,
                    phone: null,
                    roleId: null,
                    status: null,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    username: [
                        { required: true, message: "用户名不能为空", trigger: "blur" }
                    ],
                    password: [
                        { required: true, message: "密码不能为空", trigger: "blur" }
                    ],
                    email: [
                        { required: true, message: "电子邮件不能为空", trigger: "blur" },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ],
                    phone: [
                        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
                    ]
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询用户列表 */
            getList() {
                this.loading = true;
                api.listUser(this.queryParams).then(response => {
                    this.userList = response.data.rows;
                    this.total = response.data.total;
                    this.loading = false;
                }).catch(() => {
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    userId: null,
                    username: null,
                    password: null,
                    email: null,
                    phone: null,
                    roleId: null,
                    status: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.userId);
                this.single = selection.length !== 1;
                this.multiple = !selection.length;
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加用户";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const userId = row.userId || this.ids[0];
                api.getUser(userId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改用户";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.userId != null) {
                            api.updateUser(this.form).then(response => {
                                this.$message.success("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            api.addUser(this.form).then(response => {
                                this.$message.success("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const userIds = row.userId || this.ids;
                this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return api.delUser(userIds);
                }).then(() => {
                    this.getList();
                    this.$message.success("删除成功");
                }).catch(() => {});
            },
            // 切换搜索显示
            toggleSearch() {
                this.showSearch = !this.showSearch;
            },
            // 分页大小变化
            handleSizeChange(val) {
                this.queryParams.pageSize = val;
                this.getList();
            },
            // 当前页变化
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.getList();
            },
            // 重置表单
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            }
        }
    });
</script>
</body>
</html>